/**
 * @Author : andy.wangzhh
 * @Timestamp : 2016-07-23
 */
var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var common = require("common");
var nf = sm("do_Notification");
var http = require("helper/http");
var urlUser = require("url/user");
var userUtil = require("helper/user");

var pageNick=page.getData().nick;
var strPwd=page.getData().strPwd;
page.on("back", function() {
	app.closePage({"isLogin":"1"});
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({"labelTitle" : "设置初始信息"});
header.refreshData();

//右上角-发布（提交）
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "提交";
headerRight_lableNext.visible = true;

var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePageToID("","","qquan_page_index");//返回index
});

headerRight.on("touch", function() {
    initUser();
});

ui("labelPwd").text = strPwd;
ui("labelNick").text = pageNick;


var layoutForm = ui("layoutForm");
var addpassword = layoutForm.add("password", "source://view/reuse/textField.ui",0,0);
var password = ui("password");
password.bindData(hashdata);
hashdata.addData({
	"textFieldPassword" : true,
    "textFieldHint" : "输入新密码"
});
password.refreshData();

var addRepassword = layoutForm.add("repassword", "source://view/reuse/textField.ui","password");
var repassword = ui("repassword");
repassword.bindData(hashdata);
hashdata.addData({
	"textFieldPassword" : true,
    "textFieldHint" : "再确认输入密码"
});
repassword.refreshData();


var addNick = layoutForm.add("nick", "source://view/reuse/textField.ui","repassword");
var nick = ui("nick");
nick.bindData(hashdata);
hashdata.addData({
	"textFieldPassword" : false,
    "textFieldHint" : "请输入您想用的昵称"
});
nick.refreshData();

ui("btnIngore").on("touch", function(data,e){
	app.closePageToID("","","qquan_page_index");//返回index
});
ui("btnSubmit").on("touch", function(data,e){
	initUser();
});
function initUser(){
	var repwdValue = ui(addRepassword+".textField").text;
	var pwdValue = ui(addpassword+".textField").text;
	var nickValue = ui(addNick+".textField").text;
	var tips = [];
	if(repwdValue.trim()===""&&pwdValue.trim()===""&&nickValue.trim()===""){
		nf.toast("亲，密码和昵称随便填一个吧");
		return;
	}
	if(repwdValue.trim()!==""||pwdValue.trim()!==""){
		if(pwdValue.trim()!==repwdValue.trim()){
			tips.push("两次输入的密码不一致");
		}
	}
	if(nickValue.trim()!==""){
		if(nickValue.trim().length<2){
			tips.push("昵称太短了，长度要大于2");
		}
		if(nickValue.trim().length>8){
			tips.push("昵称太长了，长度不能超过8");
		}
	}
	if(tips.length!=0){
		nf.alert(tips.join("\n"));
		return;
	}
	http.post(urlUser.url.login.initUser,{"nick":nickValue,"password":pwdValue},function(data){
		app.fire("fireUserUpdate",{"logo":"","nick":nickValue});
		nf.toast("修改成功");
		app.closePageToID("","","qquan_page_index");//返回index
	});
}